//
//  SmileViewController.m
//  FVQuickMessage
//
//  Created by dllo on 16/8/22.
//  Copyright © 2016年 zty. All rights reserved.
//

#import "SmileViewController.h"
#import "NetTool.h"
#import "SmileModel.h"
#import "MovieTableViewCell.h"
#import "ImageTableViewCell.h"
#import "GIFTableViewCell.h"
#import "TextTableViewCell.h"
#import "HTMLTableViewCell.h"
#import "LHMGetHeightTool.h"
#import <CoreMotion/CoreMotion.h>


@interface SmileViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (nonatomic, retain) NSMutableArray *modelArray;

@property (nonatomic, retain) UITableView *tableView;

@property (nonatomic, retain) CMMotionManager *manger;

@end

@implementation SmileViewController

static int i = 0;

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    
    [self config];
    
    [self createSubViews];
    
    [self hanleData];
    
    // Do any additional setup after loading the view.
}

-(void)config {
    
    self.modelArray = [[NSMutableArray alloc] init];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"笑一笑";
    self.navigationController.navigationBar.translucent = NO;
    [self theShake];
    
}



-(void)hanleData {
    
    [NetTool GET:@"http://d.api.budejie.com/topic/audit-topic/1/bs0315-iphone-4.3/0-20.json?appname=bs0315&asid=0B7202DD-B713-46CB-A45F-37BBA45115FC&client=iphone&device=ios%20device&from=ios&jbk=0&mac=&market=&openudid=d41d8cd98f00b204e9800998ecf8427ec1af63a0&udid=&uid=18807655&ver=4.3" body:nil response:FVJSON headerFile:@{@"Cookie":@"j=sl9YbnqJmfrEENTn2MZlUx3AlVHbM2f3vYwb6Z3%2BVK4%3D"} success:^(id result) {
        
        NSArray *listArray = [result objectForKey:@"list"];
        
        for (NSDictionary *dic in listArray) {
            
            SmileModel *model = [[SmileModel alloc] init];
            
            [model setValuesForKeysWithDictionary:dic];
            
            [self.modelArray addObject:model];
            
        }
        
        [self.tableView reloadData];
        
    } failure:^(NSError *error) {
        
        NSLog(@"%@", error);
        
    }];
    

    
}

- (BOOL)canBecomeFirstResponder
{
    return YES;
}

-(void)theShake {
    
    if (!self.manger.isAccelerometerAvailable) {
        NSLog(@"打开错误！");
        return;
    }
    
    self.manger.accelerometerUpdateInterval = 0.1;
    
    [self.manger startAccelerometerUpdatesToQueue:[NSOperationQueue mainQueue] withHandler:^(CMAccelerometerData * _Nullable accelerometerData, NSError * _Nullable error) {
        
        if (fabs(accelerometerData.acceleration.x) > 2.0 || fabs(accelerometerData.acceleration.y) > 2.0 || fabs(accelerometerData.acceleration.z) > 2.0) {
            
            if (i == 19) {
                UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提醒" message:@"审核完毕，是否继续！" preferredStyle:UIAlertControllerStyleAlert];
                
                UIAlertAction *action = [UIAlertAction actionWithTitle:@"么么哒，继续" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    [self.modelArray removeAllObjects];
                    [self hanleData];
                    i = 0;
                    
                }];
                
                UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"不了 " style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                    
                    i = 0;
                    [self.navigationController popToRootViewControllerAnimated:YES];
                    
                }];
                
                [alert addAction:action];
                [alert addAction:action1];
                
                [self presentViewController:alert animated:YES completion:^{
                    
                }];
                
                
                
            } else {
                i++;
                MovieTableViewCell *cell = [[MovieTableViewCell alloc] init];
                
                cell.isSelect = YES;
                [self.tableView reloadData];
                
                
            }
            
        }
        
    }];
    
    
    
}

-(CMMotionManager *)manger {
    
    if (_manger == nil) {
        self.manger = [[CMMotionManager alloc] init];
    }
    return _manger;
    
}

-(void)createSubViews {
    
    self.tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
    
    [self.view addSubview:_tableView];
    
    self.tableView.showsVerticalScrollIndicator = NO;
    
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [_tableView registerClass:[GIFTableViewCell class] forCellReuseIdentifier:@"gifCell"];
    
    [_tableView registerClass:[MovieTableViewCell class] forCellReuseIdentifier:@"movieCell"];
    
    [_tableView registerClass:[TextTableViewCell class] forCellReuseIdentifier:@"textCell"];
    
    [_tableView registerClass:[ImageTableViewCell class] forCellReuseIdentifier:@"imageCell"];
    
    [_tableView registerClass:[HTMLTableViewCell class] forCellReuseIdentifier:@"htmlCell"];
    

    
    
    
    
}

-(void)next:(UIButton *)button {
    
    
    if (i == 19) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提醒" message:@"审核完毕，是否继续！" preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"么么哒，继续" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            [self.modelArray removeAllObjects];
            [self hanleData];
            i = 0;
            
        }];
        
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"不了 " style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
            i = 0;
            [self.navigationController popToRootViewControllerAnimated:YES];
            
        }];
        
        [alert addAction:action];
        [alert addAction:action1];
        
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
        
        
    } else {
        i++;
        MovieTableViewCell *cell = [[MovieTableViewCell alloc] init];
        
        cell.isSelect = YES;
        [self.tableView reloadData];
        
    }
    
    
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    if (self.modelArray.count) {
        return 1;
    }
    
    return 0;
    
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SmileModel *model = [self.modelArray objectAtIndex:indexPath.row + i];
    
    
    if ([model.type isEqualToString:@"gif"]) {
        
        GIFTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"gifCell"];
        
        cell.smileModel = model;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
    } else if ([model.type isEqualToString:@"video"]) {
        
        MovieTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"movieCell"];
        
        cell.smileModel = model;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
    } else if ([model.type isEqualToString:@"image"]) {
        
        ImageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"imageCell"];
        
        cell.smileModel = model;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
    } else if ([model.type isEqualToString:@"text"]) {
        
        TextTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"textCell"];
        
        cell.smileModel = model;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
    } else {
        
        HTMLTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"htmlCell"];
        
        cell.smileModel = model;
        
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        return cell;
        
    }
    
    
    
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    SmileModel *model = [self.modelArray objectAtIndex:indexPath.row + i];
    
    if ([model.type isEqualToString:@"gif"]) {
        
        CGFloat titleHeight = [LHMGetHeightTool heightWith:model.text];
        
        return titleHeight + 250;
        
    } else if ([model.type isEqualToString:@"video"]) {
        
        CGFloat titleHeight = [LHMGetHeightTool heightWith:model.text];
        
        return titleHeight + 350;
        
    } else if ([model.type isEqualToString:@"image"]) {
        
        CGFloat titleHeight = [LHMGetHeightTool heightWith:model.text];
        
        NSArray *imageArray = [model.image objectForKey:@"big"];
        
        NSString *url = [imageArray objectAtIndex:0];
        
        CGFloat imageHeight = [LHMGetHeightTool twoHeightWith:url];
        
        return imageHeight + titleHeight + 50;
        
    } else if ([model.type isEqualToString:@"text"]) {
        
        CGFloat titleHeight = [LHMGetHeightTool heightWith:model.text];
        
        return titleHeight + 50;
        
    } else {
        
        CGFloat titleHeight = [LHMGetHeightTool heightWith:model.text];
        
        return titleHeight + 250;
        
    }
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
